package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.context.Config;
import com.hp.dao.ResourcesDao;
import com.hp.dao.ResourcesTypeDao;
import com.hp.entity.Resources;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.ResourcesService;
import com.hp.utils.UUIDBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Transactional
@Service
public class ResourcesServiceImpl implements ResourcesService {

    @Autowired
    private ResourcesDao resourcesDao;
    @Autowired
    private ResourcesTypeDao resourcesTypeDao;

    /**
     * 添加资源
     *
     * @param file
     * @param resources
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public SubmitResultInfo saveresource(CommonsMultipartFile file, Resources resources, HttpServletRequest request) throws Exception {
        if (request == null) {
            ResultUtil.createFail(Config.MESSAGE, 211,
                    null);
        }

        // 先处理上传的图片
        if (file != null && !file.isEmpty()) {
            String realPath = request.getSession().getServletContext()
                    .getRealPath("//upload//");
//            String realPath=Config.SaveUrl+ "//upload//";
            String typename = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1);
            String name = UUIDBuild.getUUID() + "." + typename;

            File f = new File(realPath + "//" + name);
            file.transferTo(f);
            resources
                    .setResourceurl(Config.BaseUrl + "upload//" + name);
            resources.setResource_type(3);
        } else {
            resources.setResource_type(1);
        }

        // 保存资源
        resourcesDao.add(resources);

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 661, new Object[]{"资源"}));
    }


    /**
     * 资源审核列表
     *
     * @param page
     * @param rows
     * @param resources
     * @return
     * @throws Exception
     */
    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo getAllAndIsN(int page, int rows, Resources resources) throws Exception {
        Page<Resources> startPage = PageHelper.startPage(page,
                rows);
        List<Resources> pp = resourcesDao.getAllAndIsN(resources);
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());
        dataGridResultInfo.setTotal((int) startPage.getTotal());
        return dataGridResultInfo;
    }


    @Transactional(readOnly = true)
    @Override
    public Resources findByid(int id) throws Exception {
        return resourcesDao.findById(id + "");
    }

    @Override
    public SubmitResultInfo update(CommonsMultipartFile file, Resources resources, HttpServletRequest request) throws Exception {
        if (resources == null) {
            ResultUtil.createFail(Config.MESSAGE, 211,
                    null);
        }
        // 先处理上传的图片
        if (file != null && !file.isEmpty()) {
            String realPath = request.getSession().getServletContext()
                    .getRealPath("/upload/");
            String typename = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1);
            String name = UUIDBuild.getUUID() + "." + typename;

            File f = new File(realPath + "//" + name);
            file.transferTo(f);
            resources.setResourceurl(Config.BaseUrl + "upload//" + name);
        }

        // 保存资源

        resourcesDao.update(resources);

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 666, new Object[]{"资源"}));
    }

    @Override
    public SubmitResultInfo adoptpsr(Resources resources) throws Exception {
        resourcesDao.updateStatus(resources);

        if(resources.getStatus()==1){
            Resources psr = resourcesDao.findById(resources.getId()+"");
            resourcesTypeDao.addsum(psr.getTypeid());//类型对应加一
        }

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 666, new Object[]{"资源"}));
    }

    @Override
    public SubmitResultInfo deleteIsNpsr(int id) throws Exception {
        resourcesDao.deleteById(id);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 662, new Object[]{"资源"}));
    }



    @Transactional(readOnly = true)
    @Override
    public List<Resources> getAllResourceAndIsY() throws Exception {
        return resourcesDao.getAllResourceAndIsY();
    }
    @Transactional(readOnly = true)
    @Override
    public List<Resources> findAllByIdAndIsY(String typeid) throws Exception {
        return resourcesDao.findAllByIdAndIsY(typeid);
    }

    @Override
    public Resources lookById(String id) throws Exception {
        resourcesDao.addclick(id);
        return resourcesDao.findById(id);
    }

    @Override
    public DataGridResultInfo getAllById(String typeid, int page, int rows, Resources resources) throws Exception {
        Page<Resources> startPage = PageHelper.startPage(page,
                rows);
        resourcesDao.findAllByIdAndIsY(typeid);
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());
        dataGridResultInfo.setTotal((int) startPage.getTotal());
        return dataGridResultInfo;
    }
}
